package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.DeviceCond;
import com.k2data.k2app.mapper.DeviceMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.ConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lidong9144@163.com 17-3-17.
 */
@Api
@RestController
@RequestMapping("/conditions")
public class ConditionController {

    private final ConditionService conditionService;
    private final DeviceMapper deviceMapper;

    @Autowired
    public ConditionController(ConditionService conditionService, DeviceMapper deviceMapper) {
        this.conditionService = conditionService;
        this.deviceMapper = deviceMapper;
    }

    @ApiOperation(value = "字典查询条件")
    @ApiImplicitParam(name = "types", value = "类型(location, industry, customerType, dataSource,, faultType, customerNo, variableType)",
            required = true, allowMultiple = true, dataType = "string", paramType = "query")
    @GetMapping(value = "")
    @Deprecated
    public ResponseEntity<CommonResultResponse> getConditions(@RequestParam("types") List<String> types) throws Exception {
        return ResponseHelper.success(conditionService.getCondition(types));
    }

    @ApiOperation(value = "device 查询条件, 可分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "deviceNo", value = "按 deviceNo 右模糊查询", dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/device")
    @Deprecated
    public ResponseEntity<CommonResultResponse> getDeviceConditions(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "deviceNo", required = false) String deviceNo) throws Exception {
        DeviceCond cond = new DeviceCond();
        cond.setLabelLike(deviceNo);
        cond.setIsRoot(Constant.YES);

        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);

        return ResponseHelper.success(deviceMapper.selectDeviceCondition(cond));
    }

}
